package org.vt.ece4564.pcw.textinclass;

import org.apache.http.cookie.Cookie;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.vt.ece4564.pcw.textinclass.R;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.ToggleButton;
import android.app.Activity;
import android.content.Intent;

public class LoggedInActivity extends Activity implements OnClickListener {
	
	private Cookie cookie_;
	
	private ToggleButton toggleButton_;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logged_in);
        
       Intent intent = getIntent();
       String name = intent.getStringExtra("cookieName");
       String value = intent.getStringExtra("cookieValue");
       
       cookie_ = new BasicClientCookie(name, value);
       
       toggleButton_ = (ToggleButton) findViewById(R.id.loggedInStartServiceToggle);
       toggleButton_.setOnClickListener(this);
        
    }

	public void onClick(View v) {
		
		Button clicked = (Button) v;
		
		if (clicked == toggleButton_) {

			RadioButton thirtySecondsButton = (RadioButton) findViewById(R.id.loggedInRadioThirtySeconds);
			RadioButton oneMinuteButton = (RadioButton) findViewById(R.id.loggedInRadioOneMinute);
			RadioButton fiveMinutesButton = (RadioButton) findViewById(R.id.loggedInRadioFiveMinutes);
			
			if (toggleButton_.isChecked()) {
				int duration = 0;
				
				if (thirtySecondsButton.isChecked()) {
					duration = 30;
				}
				else if (oneMinuteButton.isChecked()) {
					duration = 60;
				}
				else if (fiveMinutesButton.isChecked()) {
					duration = 300;
				}
				
				thirtySecondsButton.setEnabled(false);
				oneMinuteButton.setEnabled(false);
				fiveMinutesButton.setEnabled(false);
				
				Intent intent = new Intent(this, MessengerService.class);
				intent.putExtra("refreshRate", duration);
				intent.putExtra("cookieName", cookie_.getName());
				intent.putExtra("cookieValue", cookie_.getValue());
				
				startService(intent);
				
				//Toast toast = Toast.makeText(getApplicationContext(), "ON: " + duration, Toast.LENGTH_SHORT);
				//toast.show();
			}
			else {
				thirtySecondsButton.setEnabled(true);
				oneMinuteButton.setEnabled(true);
				fiveMinutesButton.setEnabled(true);
				
				Intent intent = new Intent(this, MessengerService.class);
				
				stopService(intent);
				
				//Toast toast = Toast.makeText(getApplicationContext(), "OFF", Toast.LENGTH_SHORT);
				//toast.show();
			}
		}
		
	}
}
